
package com.autoMobile.servlet.rrm;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.autoMobile.bean.rrmBx;
import com.autoMobile.dao.rrmbxDao;

@WebServlet("/addbdServlet")
public class addbdServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 接收用户请求
        request.setCharacterEncoding("UTF-8");

        // 获取参数
        String bdh = request.getParameter("bdh");
        String bxzl = request.getParameter("bxzl");
        String beStr = request.getParameter("be");
        String bxgs = request.getParameter("bxgs");
        String csrqStr = request.getParameter("csrq");
        String jzrqStr = request.getParameter("jzrq");
        String cph = request.getParameter("cph");
        String cx = request.getParameter("cx");
        String ys = request.getParameter("ys");
        String cjh = request.getParameter("cjh");
        String csrq = request.getParameter("csrq");
        String jzrq = request.getParameter("jzrq");
        // 转换参数类型
        double be = 0.0;

        // 设置默认值和异常处理
        try {
         // 保额(be)转为 double
            System.out.println("Checking beStr...");
            if (beStr != null && !beStr.isEmpty()) {
                try {
                    be = Double.parseDouble(beStr);
                    System.out.println("beStr成功");
                } catch (NumberFormatException e) {
                    request.setAttribute("message", "保额格式错误");
                    request.getRequestDispatcher("user/addBx.jsp").forward(request, response);
                    return; // 停止进一步执行
                }
            } else {
                System.out.println("beStr为空");
            }


        } catch (Exception e) {
            e.printStackTrace();
            // 如果有其他异常，跳转到错误页面
            request.setAttribute("message", "数据处理失败，请重试");
            request.getRequestDispatcher("user/addBx.jsp").forward(request, response);
            return;
        }
        
        // 通过以上参数构造 Bx 对象
        rrmBx bx = new rrmBx(bdh, bxzl, be, bxgs, csrq, jzrq,cph,cx,ys,cjh);

        // 操作数据库
        rrmbxDao dao = new rrmbxDao();
        int i = dao.addBx(bx);
        System.out.println("发送到DAO层成功Servlet");

        // 判断是否插入成功
        if (i == 1) {
        	 System.out.println("发送到DAO层失败");
            request.setAttribute("message", "保单号已经存在");
            request.getRequestDispatcher("BxServlet").forward(request, response);
        } else {
        	 System.out.println("发送到DAO层成功");
            response.sendRedirect("BxServlet");
            request.setAttribute("message", "1");
        }
    }
}